package com.tianxiao.test.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import com.baijia.tianxiao.sqlbuilder.SingleSqlBuilder;
import com.baijia.tianxiao.sqlbuilder.support.JdbcTemplateDaoSupport;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import com.tianxiao.test.dao.BackLogDao;
import com.tianxiao.test.enums.BackLogStatus;
import com.tianxiao.test.model.BackLog;

/**
 * 待办事项
 * @author zhangbing
 *
 */
@Repository
public class BackLogDaoImpl extends JdbcTemplateDaoSupport<BackLog> implements
		BackLogDao {

	@Override
	public List<BackLog> getEffectiveBackLog(Collection<Long> userIds, Date expireTime, Integer isCompleted) {
		SingleSqlBuilder<BackLog> builder = createSqlBuilder();
		if (CollectionUtils.isNotEmpty(userIds)) {
			builder.in("userId", userIds);
		}
		if (expireTime != null) {
			builder.gt("expireTime", expireTime);
		}
		if (isCompleted != null) {
			builder.eq("status", isCompleted);
		}
		return queryList(builder);
	}

	@Override
	public List<BackLog> getExpireTodayBackLog(Collection<Long> userIds,
			Date expireStart, Date expireEnd, Integer isCompleted) {
		SingleSqlBuilder<BackLog> builder = createSqlBuilder();
		builder.in("userId", userIds);
		builder.between("expireTime", expireStart, expireEnd);
		if (isCompleted != null) {
			builder.eq("status", isCompleted);
		}
		
		return queryList(builder);
	}

	@Override
	public List<BackLog> getNoticeBackLogList(Date startTime, Date endTime) {
		SingleSqlBuilder<BackLog> builder = createSqlBuilder();
		builder.between("noticeTime", startTime, endTime);
		builder.ne("status", BackLogStatus.EFFECTIVE.getStatus());
		
		return queryList(builder);
	}
	
    @Override
    public Map<Long, Long> getUserLogCount(Date startTime, Date endTime, Integer status, Collection<Long> userIds) {
        Preconditions.checkArgument(startTime != null || endTime != null, "startTime and endTime is null");

        Map<String, Object> params = Maps.newHashMap();
        StringBuffer sql = new StringBuffer();
        sql.append("select user_id as userId, count(id) as logCount from virgo.back_log where");

        if (startTime != null && endTime != null) {
            sql.append(" notice_time between :startTime and :endTime");
            params.put("startTime", startTime);
            params.put("endTime", endTime);
        } else if (startTime == null && endTime != null) {
            sql.append(" and noticeTime < :endTime");
            params.put("endTime", endTime);
        } else if (startTime != null && endTime == null) {
            sql.append(" and noticeTime > :startTime");
            params.put("startTime", startTime);
        }
        if(status != null){
            sql.append(" and status = :status");
            params.put("status", status);
        }
        if(CollectionUtils.isNotEmpty(userIds)){
            sql.append(" and user_id in (:userIds )");
            params.put("userIds", userIds);
        }
        sql.append(" group by userId");

        Map<Long, Long> userLogCountMap =
            this.getNamedJdbcTemplate().query(sql.toString(), params, new ResultSetExtractor<Map<Long, Long>>() {
                @Override
                public Map<Long, Long> extractData(ResultSet rs) throws SQLException, DataAccessException {
                    Map<Long, Long> map = new HashMap<Long, Long>();
                    while (rs.next()) {
                        map.put(rs.getLong("userId"), rs.getLong("logCount"));
                    }
                    return map;
                }
            });
        return userLogCountMap;
    }
}
